<template>
  <div>
    <el-dialog
      v-model="isDialog"
      title="批量导入客户"
      destroy-on-close
      width="460px"
      :before-close="cancelExport"
    >
      <div class="uploadExcel">
        <div style="margin-bottom: 20px;">
          <span>客户导入标识：</span>
          <el-select
            v-model="importTypeCode"
            placeholder="请选择"
          >
            <el-option
              v-for="item in importList"
              :key="item.importTypeCode"
              :label="item.name"
              :value="item.importTypeCode"
            />
          </el-select>
        </div>
        <div style="margin-bottom: 20px;">
          <span>导入方式：</span>
          <el-radio-group v-model="importType">
            <el-radio :label="0">
              Excel表格模板
            </el-radio>
            <el-radio :label="1">
              文本导入
            </el-radio>
          </el-radio-group>
        </div>
        <div v-if="importType==0">
          <div class="tips">
            点击下载<span @click="download">Excel表格模板</span>
          </div>
          <div style="margin-bottom: 20px;">
            <span>重复客户处理：</span>
            <el-select
              v-model="isCover"
              placeholder="请选择"
            >
              <el-option
                v-for="item in isCoverLsit"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </div>
          <el-upload
            v-if="isDialog"
            ref="upload"
            class="upload"
            drag
            :limit="1"
            action=""
            accept=".xlsx,.xls"
            :show-file-list="true"
            :http-request="upload"
          >
            <div class="uploadSlot">
              <p>点击或文件拖拽到这里上传</p>
              <div class="uploadSlot-tip">
                *仅支持xlsx格式，请将大小控制在20M以内
              </div>
            </div>
          </el-upload>
        </div>
        <div v-else>
          <el-input
            v-model.trim="userUniqueList"
            type="textarea"
            placeholder="请输入"
            :autosize="{ minRows: 3, maxRows: 5 }"
            show-word-limit
          />
          <div class="tips">
            ​多条数据请用分号分开,例如15534345656;16532324344
          </div>
        </div>
      </div> 
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancelExport()">取 消</el-button>
          <el-button
            type="primary"
            @click="submit"
          >确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog
      v-model="isResultDialog"
      title="批量导入结果详情"
      width="800px"
      :before-close="closeResultExport"
    >
      <div class="uploadExcel">
        <div class="resultTips">
          <el-result
            v-if="result.userExistCount + result.lostPhoneOrUnionIdCount > 0"
            icon="error"
          />
          <el-result
            v-else
            icon="success"
          />
          <span v-if="result.saveDBNum > 0">
            导入成功<span class="success">{{ result.saveDBNum }}</span>条</span>
          <span v-if="result.updateDBNum > 0">
            更新成功<span class="success">{{ result.updateDBNum }}</span>条</span>
          <span
            v-if="result.userExistCount + result.lostPhoneOrUnionIdCount > 0"
          >
            导入失败<span class="fail">{{
              result.userExistCount + result.lostPhoneOrUnionIdCount
            }}</span>条</span>
        </div>
        <div v-if="result.messages.length > 0">
          <el-table
            :data="result.messages"
            stripe
          >
            <el-table-column
              prop="userGroupName"
              label="客群名称"
              min-width="100"
            />
            <el-table-column
              prop="phoneOrUnionId"
              label="手机号"
              min-width="100"
            />
            <el-table-column
              prop="msg"
              label="导入失败原因"
              min-width="180"
            />
          </el-table>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeResultExport()">完成</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import CustomerApi from "@/api/customerApi";
import { fileDownload } from "@/utils"
export default {
  props: {
    isExport: {
      require: false,
      type: Boolean,
      default: () => {
        return false;
      },
    },
  },
  data() {
    return {
      result: {
        saveDBNum: 0,
        userExistCount: 0,
        lostPhoneOrUnionIdCount: 0,
        messages: [],
      },
      importType: 0,
      userUniqueList: '',
      importTypeCode: 'phone',
      tableData: [],
      importList: [ {
        importTypeCode: 'phone',
        name: '手机号',
        url: './fileTemplate/customerList.xlsx'
      } ],
      isResultDialog: false,
      isDialog: false,
      isCover: 1,
      form: {
        file: "",
      },
      isCoverLsit: [
        { id: 1, name: "覆盖已有数据" },
        { id: 0, name: "不导入" },
      ],
    };
  },
  watch: {
    isExport(val) {
      this.isDialog = val;
    },
  },
  mounted(){
    this.getList()
  },
  methods: {
    async download() {
      let row = this.importList.find((ele) => {
        if (ele.importTypeCode == this.importTypeCode){
          return ele
        }
      })
      fileDownload(row.url, '客户列表.xlsx')
    },
    upload(e) {
      let file = e.file;
      this.form.file = file;
      // this.$set(this.form, "file", file);
    },
    async submit() {
      let rs
      if (this.importType == 0){
        console.log("[ this.$route.params.id ] >", this.$route.params.id);
        let formdata = new FormData();
        formdata.append("userGroupId", this.$route.params.id);
        console.log("[ this.formdata ] >", formdata);
        formdata.append("isCover", this.isCover);
        formdata.append("importTypeCode", this.importTypeCode);
        console.log("this.form", this.form);
        for (const key in this.form) {
          if (Object.hasOwnProperty.call(this.form, key)) {
            const element = this.form[key];
            formdata.append(key, element);
          }
        }
        rs = await CustomerApi.exportCustomerExcel(formdata);
      } else {
        let param = {
          userGroupId: this.$route.params.id,
          userUniqueList: this.userUniqueList.split(";"),
          importTypeCode: this.importTypeCode
        }
        rs = await CustomerApi.textAdd(param);
      }

      if (rs.code === 200) {
        this.result = rs.payload;
        this.isDialog = false;
        this.isResultDialog = true;
      }
    },
    cancelExport() {
      if (this.$refs.upload) {
        this.$refs.upload.clearFiles();
      }
      this.form = {
        file: "",
      };
      this.importType = 0
      this.userUniqueList = ''
      this.$emit("cancelExport");
    },
    confirmExoprt() {
      this.$emit("confirmExoprt");
      this.isResultDialog = false;
      this.cancelExport();
    },
    closeResultExport() {
      this.confirmExoprt();
    },
    async getList(){
      let rs = await CustomerApi.thirdLogin();
      if (rs.code === 200) {
        rs.payload = rs.payload ?? []
        let temp = [] 
        rs.payload.forEach(i => {
          temp.push({
            importTypeCode: i.externalPlatformType,
            name: i.externalPlatformName,
            url: './fileTemplate/customerIDList.xlsx'
          })
        })
        this.importList = this.importList.concat(temp)
      }
    }
  }
};
</script>


<style lang="scss" scoped>
.tips {
  font-size: 12px;
  font-weight: 400;
  color: #999999;
  line-height: 17px;
  margin-bottom: 8px;
  padding-left: 5px;
  span {
    color: #6178ff;
    cursor: pointer;
  }
}
.upload {
  width: 412px;
  margin-top: 20px;
  :deep(.el-upload-dragger) {
    width: 100%;
    border: 0;
    padding: 0;
    border-radius: 2px;
   
    // border-bottom: 1px solid #dae0ff;
  }
}
.uploadSlot {
  width: 100%;
  height: 85px;
   background: url("./../../../../assets/images/_upload_icon.png") center / 100% no-repeat;
  padding-top: 102px;
  text-align: center;
  p {
    font-size: 16px;
    color: #333333;
    line-height: 24px;
    margin-bottom: 4px;
  }
  &-tip {
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    line-height: 22px;
  }
}
.message-style .el-message-box__content {
  max-height: 450px;
  overflow: hidden;
  overflow-y: scroll;
}
.el-select {
  width: 314px;
}
</style>
